<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*
            new Promise中的逻辑就是：
                1.输出当前灯色
                2.一定时间之后resolve
        */
        function timer(color, delay, next) {
            return new Promise((resolve) => {
                console.log(`当前是${color}灯，${delay}秒后变成${next}灯`);
                setTimeout(() => {
                    resolve();
                }, delay * 1000);
            })
        }

        /*
            因为timer一定时间之后才会resolve，直接await罗列执行顺序即可
        */
        async function run() {
            await timer("红", 3, '绿');
            await timer("绿", 5, '黄');
            await timer("黄", 2, "红");
            run();
        }

        run();
    </script>
</body>
</html>